package com.ruoyi.comprehensive.service.impl;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.comprehensive.domain.PetitionLetter;
import com.ruoyi.comprehensive.domain.PetitionLetterProcess;
import com.ruoyi.comprehensive.mapper.PetitionLetterMapper;
import com.ruoyi.comprehensive.mapper.PetitionLetterProcessMapper;
import com.ruoyi.comprehensive.service.IPetitionLetterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 网上信访Service业务层处理
 *
 * @author Allen
 * @date 2021-08-31
 */
@Service
public class PetitionLetterServiceImpl implements IPetitionLetterService {
    @Autowired
    private PetitionLetterMapper petitionLetterMapper;

    @Autowired
    private PetitionLetterProcessMapper petitionLetterProcessMapper;

    /**
     * 查询网上信访
     *
     * @param id 网上信访主键
     * @return 网上信访
     */
    @Override
    public PetitionLetter selectPetitionLetterById(Long id) {
        PetitionLetter petitionLetter = petitionLetterMapper.selectPetitionLetterById(id);
        PetitionLetterProcess petitionLetterProcess = new PetitionLetterProcess();
        petitionLetterProcess.setPetitionLetterId(id);
        List<PetitionLetterProcess> processeList = petitionLetterProcessMapper.selectPetitionLetterProcessList(petitionLetterProcess);
        petitionLetter.setProcessList(processeList);
        return petitionLetter;
    }

    /**
     * 查询网上信访列表
     *
     * @param petitionLetter 网上信访
     * @return 网上信访
     */
    @Override
    public List<PetitionLetter> selectPetitionLetterList(PetitionLetter petitionLetter) {
        return petitionLetterMapper.selectPetitionLetterList(petitionLetter);
    }

    /**
     * 新增网上信访
     *
     * @param petitionLetter 网上信访
     * @return 结果
     */
    @Override
    public int insertPetitionLetter(PetitionLetter petitionLetter) {
        Long userId = SecurityUtils.getUserId();
        petitionLetter.setSoldierId(userId);
        petitionLetter.setStatus(1);
        petitionLetter.setPetitionTime(new Date());
        return petitionLetterMapper.insertPetitionLetter(petitionLetter);
    }

    /**
     * 修改网上信访
     *
     * @param petitionLetter 网上信访
     * @return 结果
     */
    @Override
    public int updatePetitionLetter(PetitionLetter petitionLetter) {
        return petitionLetterMapper.updatePetitionLetter(petitionLetter);
    }

    /**
     * 批量删除网上信访
     *
     * @param ids 需要删除的网上信访主键
     * @return 结果
     */
    @Override
    public int deletePetitionLetterByIds(Long[] ids) {
        return petitionLetterMapper.deletePetitionLetterByIds(ids);
    }

    /**
     * 删除网上信访信息
     *
     * @param id 网上信访主键
     * @return 结果
     */
    @Override
    public int deletePetitionLetterById(Long id) {
        return petitionLetterMapper.deletePetitionLetterById(id);
    }

    @Override
    public int replay(PetitionLetterProcess petitionLetterProcess) {
        //信访状态改为处理中
        PetitionLetter petitionLetter = petitionLetterMapper.selectPetitionLetterById(petitionLetterProcess.getPetitionLetterId());
        if (Objects.isNull(petitionLetter)) {
            return 0;
        }
        updatePetitionLetterStatus(petitionLetter.getId(), 2);
        //构建回复记录
        petitionLetterProcess.setHandleTime(new Date());
        Long userId = SecurityUtils.getUserId();
        petitionLetterProcess.setHandleUser(userId);
        if (Objects.nonNull(petitionLetter) && Objects.equals(petitionLetter.getSoldierId(), userId)) {
            petitionLetterProcess.setPetitionLetterIdentity(1);
        }else {
            petitionLetterProcess.setPetitionLetterIdentity(0);
        }
        return petitionLetterProcessMapper.insertPetitionLetterProcess(petitionLetterProcess);
    }

    @Override
    public int endPetitionLetter(Long id) {
        PetitionLetter petitionLetter = new PetitionLetter();
        petitionLetter.setId(id);
        petitionLetter.setStatus(3);
        return updatePetitionLetterStatus(id, 3);
    }

    private int updatePetitionLetterStatus(Long id, Integer status) {
        PetitionLetter petitionLetter = new PetitionLetter();
        petitionLetter.setId(id);
        petitionLetter.setStatus(status);
        return petitionLetterMapper.updatePetitionLetter(petitionLetter);
    }
}
